package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.Calendar;
import model.bean.ListCalendar;
import model.bo.CalendarBO;
import model.dao.CalendarDAO;

/**
 * Servlet implementation class CityServlet
 */
@WebServlet("/CityServlet")
public class CalendarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CalendarServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

	

		CalendarBO calendarBO = new CalendarBO();
		CalendarDAO calendarDAO = new CalendarDAO();

		// load calendar_demo.jsp
		if (request.getParameter("calendar") != null) {

			// calendar on week
			ArrayList<ListCalendar> listCalendar = calendarBO.getCalendar();

			// calendar status
			ArrayList<Calendar> listCalendarStatus = calendarBO
					.getCalendarStatus();

			request.setAttribute("listCalendarStatus", listCalendarStatus);
			request.setAttribute("listCalendar", listCalendar);

			RequestDispatcher rd = request
					.getRequestDispatcher("calendar_demo.jsp");
			rd.forward(request, response);
		}

		// load listcalendar.jsp
		else if (request.getParameter("listcalendar") != null) {

			ArrayList<Calendar> calendar = calendarBO.viewCalendar();
			request.setAttribute("calendar", calendar);

			RequestDispatcher rd = request
					.getRequestDispatcher("listcalendar.jsp");
			rd.forward(request, response);
		}

		// change listDalendar
		else if (request.getParameter("time") != null) {
			ArrayList<Calendar> calendar = calendarBO.viewCalendar(request
					.getParameter("time"));
			request.setAttribute("calendar", calendar);
			RequestDispatcher rd = request
					.getRequestDispatcher("listcalendar.jsp");
			rd.forward(request, response);
		}

		// insert calendar
		else if (request.getParameter("insert") != null) {
			
			String datetime = request.getParameter("datetime");
			String title = request.getParameter("title");
			String content = request.getParameter("content");
			String status = request.getParameter("status");
			String location = request.getParameter("location");

			request.setAttribute("insert", "insert");

			String date = datetime.split(" ")[0];
			String starttime = datetime.split(" ")[1];
			String endtime = datetime.split(" ")[5];

			boolean success = calendarBO.insertCalendar(date, starttime,
					endtime, title, content, status, location);
			request.setAttribute("success", success ? "true" : "false");

			RequestDispatcher rd = request
					.getRequestDispatcher("calendar_new.jsp");
			rd.forward(request, response);

		}

		// update Calendar
		

		// back
		else if (request.getParameter("back") != null) {

			ArrayList<Calendar> calendar = calendarBO.viewCalendar();
			request.setAttribute("calendar", calendar);

			RequestDispatcher rd = request
					.getRequestDispatcher("listcalendar.jsp");
			rd.forward(request, response);
		}

		// view calendar
		else if (request.getParameter("view") != null) {

			RequestDispatcher rd = request
					.getRequestDispatcher("calendar_new.jsp");
			rd.forward(request, response);
		}

		// new
		else if (request.getParameter("new") != null) {

			request.setAttribute("new", "new");
			RequestDispatcher rd = request
					.getRequestDispatcher("calendar_new.jsp");
			rd.forward(request, response);
		}

		// xoa calendar
		else if (request.getParameter("xoa") != null) {

			String id = request.getParameter("xoa");
			boolean deleteSuccess = calendarDAO.delete(id);
			request.setAttribute("deleteSuccess", deleteSuccess ? "true"
					: "false");

			ArrayList<Calendar> calendar = calendarBO.viewCalendar();
			request.setAttribute("calendar", calendar);

			RequestDispatcher rd = request
					.getRequestDispatcher("listcalendar.jsp");
			rd.forward(request, response);
		}

		// edit calendar
		else if (request.getParameter("edit") != null) {

			request.setAttribute("edit", "edit");

			String id = request.getParameter("id");
			Calendar calendar = calendarDAO.getCalendar(id);
			String time = calendar.getDate() + " "
					+ calendar.getStarttime().substring(0, 5) + " AM / "
					+ calendar.getDate() + " " + calendar.getEndtime().substring(0, 5) + " PM";

			request.setAttribute("time", time);
			request.setAttribute("calendar", calendar);
			RequestDispatcher rd = request.getRequestDispatcher("sua_lich.jsp");
			rd.forward(request, response);
		}

		else {
			ArrayList<Calendar> calendar = calendarBO.viewCalendar();
			request.setAttribute("calendar", calendar);

			RequestDispatcher rd = request
					.getRequestDispatcher("listcalendar.jsp");
			rd.forward(request, response);
		}

	}

}
